<?php
header('Content-Type: text/html; charset=utf-8');
include('../class/class_connect.php');
include('../includes/transliterate.php');
$aConnect=new connect('m');
if(!function_exists('mb_ucfirst')) {
    function mb_ucfirst($str, $enc = 'utf-8') {
    		return mb_strtoupper(mb_substr($str, 0, 1, $enc), $enc).mb_substr($str, 1, mb_strlen($str, $enc), $enc);
    }
}
function safetyCheck($value)
{
	$value=@ strip_tags($value);
	$value=htmlspecialchars($value);
	$value=addslashes($value);
	$value=str_replace('\'', '', $value);
	$value=substr($value, 0, 100);
	return $value;
}
foreach($_GET as $key=>$vol)
	$_GET[$key]=safetyCheck($vol);
$row=mysql_fetch_array($aConnect->select_all($_GET['city'], "WHERE id='".$_GET['id']."' AND (firms LIKE '%".$_GET['val']."%' || firms LIKE '%".mb_ucfirst($_GET['val'])."%' || firms LIKE '%".mb_strtoupper($_GET['val'], 'utf-8')."%')"));
if(!$row && $_GET['val'])
$row=mysql_fetch_array($aConnect->select_all($_GET['city'], "WHERE id='".$_GET['id']."' AND firms LIKE '%".mb_transliterate($_GET['val'])."%'"));
if($row && $_GET['val'])
{
	//$val=iconv("UTF-8", "WINDOWS-1251", $value);
	$res=array();
	$res[0]=array();
	$res[1]=array();
	$res[2]=array();
	$res[3]=array();
	$res[4]=array();
	//echo $row['firms'];
	$mallsFirmsArray=explode('|', $row['firms']);
	foreach($mallsFirmsArray as $key=>$val)
	{
		$mallsFirmsElsArray=explode('^', $val);
		for($i=0; $i<count($mallsFirmsElsArray); $i++)
		{
			if($i==0)continue;
			//echo mb_strtolower($mallsFirmsElsArray[$i]).'-'.mb_strtolower($_GET['val']).'<br>';
			/*if(strpos(mb_strtolower($mallsFirmsElsArray[$i]), mb_strtolower($_GET['val']))===0)
			{
				//echo 1;
				if(!in_array('<div class="mallsSearchRes">'.$mallsFirmsElsArray[$i].'</div>', $res))
				{
					$res[0][]='<div class="mallsSearchRes">'.$mallsFirmsElsArray[$i].'</div>';
				}
			} */
			$words=explode(' ', $mallsFirmsElsArray[$i]);
			for($j=0; $j<count($words); $j++)
			{
				if(strpos(mb_strtolower($words[$j], 'utf-8'), mb_strtolower($_GET['val'], 'utf-8'))===0)
				{
					if(!in_array('<div class="mallsSearchRes">'.$mallsFirmsElsArray[$i].'</div>', $res[$j]))
					{
						$res[$j][]='<div class="mallsSearchRes">'.$mallsFirmsElsArray[$i].'</div>';
					}
				}
				if(strpos(mb_strtolower($words[$j], 'utf-8'), mb_strtolower(mb_transliterate($_GET['val']), 'utf-8'))===0)
				{
					if(!in_array('<div class="mallsSearchRes">'.$mallsFirmsElsArray[$i].'</div>', $res[$j]))
					{
						$res[$j][]='<div class="mallsSearchRes">'.$mallsFirmsElsArray[$i].'</div>';
					}
				}
			}

		}
	}
	if($res)
	{
		if($res[0])$result=implode('^', $res[0]);
		if($res[1])$result.='^'.implode('^', $res[1]);
		if($res[2])$result.='^'.implode('^', $res[2]);
		if($res[3])$result.='^'.implode('^', $res[3]);
		if($res[4])$result.='^'.implode('^', $res[4]);
	}
	$res=explode('^', $result);
	$res=array_unique($res);
	$result=implode('', $res);
	echo $result;
}
?>